package com.design.service.impl;

import com.design.domain.form.LoginUser;
import com.design.domain.vo.SysUser;
import com.design.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServeceImpl implements UserDetailsService {

	@Autowired
	private IUserService userService;

	@Override
	public UserDetails loadUserByUsername(String loginUser) throws UsernameNotFoundException {
		List<SysUser> users = userService.getUserInfoByUserNameOrPhone(loginUser);
		if (null == users || users.size() == 0) {
			throw new UsernameNotFoundException("用户不存在");
		}
		if (users.size() > 1) {
			throw new UsernameNotFoundException("用户名不唯一，请使用手机号码登录");
		}
		SysUser sysUser = users.get(0);
		return new LoginUser(sysUser);
	}
}
